﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpLinqTeach
{
    /// <summary>
    /// 初步了解：LINQ查询的强大，同时要知道延迟查询
    /// </summary>
    internal class TestClass2
    {
        #region 1.不使用LINQ数组
        public static void Demo1()
        {
            int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

            List<int> result = new List<int>();

            //找到奇数
            foreach(int item in nums)
            {
                if(item % 2 != 0)
                {
                    result.Add(item);
                }              
            }
            result.Sort();
            result.Reverse();

            foreach (int item in result)
            {
                Console.WriteLine(item);
            }
        }
        #endregion

        #region 2.使用LINQ数组,观察查询时机（延迟执行）
        public static void Demo2()
        {
            int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

            var result = from num in nums     //查询语句
                         where num % 2 != 0
                         orderby num descending
                         select num;

            foreach(int item in result)
            {
                Console.WriteLine(item);
            }
        }
        #endregion

        #region 3.使用扩展方法Select()
        public static void Demo3()
        {
            int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

            var result = nums.Select(item => item * item);

            foreach (int item in result)
            {
                Console.WriteLine(item);
            }
        }
        #endregion

        #region 4.使用扩展方法Where()
        public static void Demo4()
        {
            int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

            var result = nums
                .Where(item => item % 2 != 0)
                .Select(item => item * item);

            foreach (int item in result)
            {
                Console.WriteLine(item);
            }
        }
        #endregion

        #region 5.使用扩展方法OrderBy()
        public static void Demo5()
        {
            int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

            var result = nums
                .Where(item => item % 2 != 0)
                .OrderBy(item => item)
                .Select(item => item * item);

            foreach (int item in result)
            {
                Console.WriteLine(item);
            }

            var result2 = nums
                .Where(item => item % 2 != 0)
                .OrderByDescending(item => item)
                .Select(item => item * item);

            Console.WriteLine("------------------------------");

            foreach (int item in result2)
            {
                Console.WriteLine(item);
            }
        }

        public static void Demo6()
        {
            string[] names = { "张大才", "王琦", "刘静", "赵鑫鑫", "杜丽丽", "马具才", "那英", "成龙" };

            var result = names
                .Where(item => item.Length == 2)
                .Select(item => item)
                .OrderByDescending(item => item.Substring(0, 1));

            foreach (string item in result)
            {
                Console.WriteLine(item);
            }
        }
        #endregion

        #region 6.使用扩展方法GroupBy()
        public static void Demo7()
        {
            string[] names = { "张大才", "王琦", "刘静", "赵鑫鑫", "杜丽丽", "马具才", "那英", "成龙" };

            var list = names
                .Where(item => item.Length == 2)
                .Select(item => item)
                .GroupBy(item => item.Substring(0, 1));

            foreach (var item in list)
            {
                Console.WriteLine("------------------------------");
                Console.WriteLine($"字段分组:{item.Key}"); //显示分组名称
                //内层循环遍历（分组项）
                foreach(var i in item)
                {
                    Console.WriteLine(i);
                }
            }
        }
        #endregion

        #region 7.使用LINQ查询方法，观察查询时机（延迟执行）
        public static void Demo8()
        {
            int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

            var result = nums
                .Where(item => item % 2 == 0)   //查询方法
                .Select(item => item * item)
                .OrderBy(item => item);

            foreach (int item in result)
            {
                Console.WriteLine(item);
            }
        }
        #endregion

        #region 8.查询的立即执行
        public static void Demo9()
        {
            int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

            var result = nums
                .Where(item => item % 2 == 0)   //查询方法
                .Select(item => item * item)
                .OrderBy(item => item)
                .Count();

            Console.WriteLine(result);
        }
        #endregion

        #region 9.查询语句与聚合函数Count
        public static void Demo10()
        {
            Student obj1 = new Student() { StudentId = 1001, StudentName = "学员1"};
            Student obj2 = new Student() { StudentId = 1009, StudentName = "学员9" };
            Student obj3 = new Student() { StudentId = 1012, StudentName = "学员12" };
            Student obj4 = new Student() { StudentId = 1003, StudentName = "学员3" };
            Student obj5 = new Student() { StudentId = 1019, StudentName = "学员19" };
            Student obj6 = new Student() { StudentId = 1006, StudentName = "学员6" };
            List<Student> sList = new List<Student>() { obj1, obj2, obj3, obj4, obj5, obj6 };

            var count1 = (from s in sList
                         where s.StudentId > 1010
                         select s).Count();            //查询语句和查询方法经常混合使用

            var count2 = sList
                .Where(c => c.StudentId > 1010)
                .Select(c => c)
                .Count();

            Console.WriteLine($"count1 = {count1}  count2 = {count2}");
        }
        #endregion

        #region 10.查询语句与聚合函数Max Min Average
        public static void Demo11()
        {
            Student obj1 = new Student() { StudentId = 1001, StudentName = "学员1" };
            Student obj2 = new Student() { StudentId = 1009, StudentName = "学员9" };
            Student obj3 = new Student() { StudentId = 1012, StudentName = "学员12" };
            Student obj4 = new Student() { StudentId = 1003, StudentName = "学员3" };
            Student obj5 = new Student() { StudentId = 1019, StudentName = "学员19" };
            Student obj6 = new Student() { StudentId = 1006, StudentName = "学员6" };
            List<Student> sList = new List<Student>() { obj1, obj2, obj3, obj4, obj5, obj6 };

            var maxAge = (from s in sList
                         select s.StudentAge).Max();

            var minAge = (from s in sList
                          select s.StudentAge).Min();

            var avgAge = (from s in sList
                          select s.StudentAge).Average();

            var sumAge = (from s in sList
                          select s.StudentAge).Sum();

        }
        #endregion

        #region 11.查询语句与排序类ThenBy的使用
        public static void Demo12()
        {
            Student obj1 = new Student() { StudentId = 1001,StudentAge=25, StudentName = "学员1" };
            Student obj2 = new Student() { StudentId = 1009,StudentAge=23, StudentName = "学员9" };
            Student obj3 = new Student() { StudentId = 1012,StudentAge=25, StudentName = "学员12" };
            Student obj4 = new Student() { StudentId = 1003,StudentAge=24, StudentName = "学员3" };
            Student obj5 = new Student() { StudentId = 1019,StudentAge=25, StudentName = "学员19" };
            Student obj6 = new Student() { StudentId = 1006,StudentAge=28, StudentName = "学员6" };
            List<Student> sList = new List<Student>() { obj1, obj2, obj3, obj4, obj5, obj6 };

            //查询语句更简单，但是排序单一
            var stuList1 = from s in sList
                          orderby s.StudentAge, s.StudentId
                          select s;

            //查询方法排序可以混合使用
            var stuList2 = sList
                .Select(s => s)
                .OrderBy(s => s.StudentAge)
                .ThenBy(s => s.StudentId)
                .ThenByDescending(s => s.StudentName);

        }
        #endregion

        #region 12.LINQ其他查询(Distinct、生成数据)
        public static void Demo13()
        {
            int[] nums = { 1, 2, 2, 6, 5, 6, 7, 8, 8 };
            var list = nums.Distinct();   //去除重复
            foreach(var item in list)
            {
                Console.WriteLine(item);
            }
        }

        public static void Demo14()
        {
            var nums1 = Enumerable.Range(1, 10);
            var nums2 = Enumerable.Repeat("LINQ best!",10);

            foreach(var item in nums1)
            {
                Console.WriteLine(item);
            }

            foreach(var item in nums2)
            {
                Console.WriteLine(item);
            }
        }
        #endregion

    }
}
